package com.android.example.app3;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.SoundPool;
import android.util.Log;
import android.util.SparseArray;

public class BeepHelper {
    // 声音类型常量

    public static final int SOUND_DI = R.raw.smdi;         // 默认"滴"声
    public static final int SOUND_DHYCZ = R.raw.rdanhaoyicunzai; //
    public static final int SOUND_DHCF = R.raw.danhaochongfu; //

    private SoundPool soundPool;
    @SuppressLint("UseSparseArrays")
    private final SparseArray<Integer> soundMap = new SparseArray<>();
    private int currentSound;
    private boolean isInitialized = false;
    private Context applicationContext; // 用于获取资源的应用上下文

    public void init(Context context, int defaultSound) {
        if (soundPool != null) {
            release();
        }

        // 保存应用上下文避免内存泄漏
        applicationContext = context.getApplicationContext();

        soundPool = new SoundPool.Builder()
                .setMaxStreams(3)
                .build();

        currentSound = defaultSound;
        loadSound(defaultSound);

        soundPool.setOnLoadCompleteListener((sp, sampleId, status) -> {
            if (status != 0) {
                Log.e("BeepHelper", "音效加载失败: " + sampleId);
            } else {
                Log.i("BeepHelper", "音效加载成功: " + sampleId);
                isInitialized = true;
            }
        });
    }

    private void loadSound(int soundResId) {
        if (soundPool != null && applicationContext != null) {
            int soundId = soundPool.load(applicationContext, soundResId, 1);
            soundMap.put(soundResId, soundId);
        }
    }

    public void preloadSound(int soundResId) {
        if (soundPool == null || soundMap.get(soundResId) != null) {
            return;
        }
        loadSound(soundResId);
        Log.i("BeepHelper", "预加载音效: " + soundResId);
    }

    public void playBeep() {
        playSound(currentSound);
    }

    public void playSound(int soundResId) {
        if (!isInitialized || soundPool == null) {
            Log.w("BeepHelper", "音效池未初始化");
            return;
        }

        Integer soundId = soundMap.get(soundResId);
        if (soundId != null && soundId != 0) {
            soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f);
        } else {
            Log.e("BeepHelper", "音效未加载: " + soundResId);
        }
    }

    public void release() {
        if (soundPool != null) {
            soundMap.clear();
            soundPool.release();
            soundPool = null;
            isInitialized = false;
        }
        applicationContext = null; // 清除上下文引用
    }

    // 修复后的资源名称获取方法
    private String getResourceName(int resId) {
        if (applicationContext == null) {
            return "ResID:" + resId;
        }

        try {
            return "R.raw." + applicationContext.getResources().getResourceEntryName(resId);
        } catch (Exception e) {
            return "ResID:" + resId;
        }
    }

    // 其他查询方法...
    public int getDefaultSound() {
        return currentSound;
    }

    public void setDefaultSound(int soundResId) {
        currentSound = soundResId;
        Log.i("BeepHelper", "默认音效设置为: " + soundResId);
    }

    public boolean isSoundLoaded(int soundResId) {
        return soundMap.get(soundResId) != null;
    }

    public int[] getLoadedSounds() {
        int size = soundMap.size();
        int[] sounds = new int[size];
        for (int i = 0; i < size; i++) {
            sounds[i] = soundMap.keyAt(i);
        }
        return sounds;
    }
}